<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
	"use strict";
	var ws = null;
	(function(){
       var target = "ws://localhost:8080/WSocket/echo";
        if ('WebSocket' in window) {
            ws = new WebSocket(target);
        } else if ('MozWebSocket' in window) {
            ws = new MozWebSocket(target);
        } else {
            alert('此浏览器不支持WebSocket.');
            return;
        }
        ws.onopen = function () {
            console.log('信息: WebSocket连接打开.');
        };
        ws.onmessage = function (event) {
        	console.log('接收: ' + event.data);
        	var texts = document.getElementById('texts');
        	texts.value += (event.data+"/n");
        };
	})();
	function mgsend(){
		if (ws != null) {
            var message = document.getElementById('message').value;
            console.log('发送: ' + message);
            ws.send(message);
        } else {
            alert('WebSocket连接没有建立, 请连接.');
        }
	}
</script>
</head>
<body>
	<h2>WS</h2>
	<input type="text" width="100" id="message"/><button onclick="mgsend();">发送</button><br/>
	<textarea rows="10" cols="80" id="texts"></textarea>
</body>
</html>
